home *** CD-ROM | disk | FTP | other *** search
/ PC Users 1999 May / Cd Pc Users extra 20 mayo 1999.iso / Prog / Inst / FTP / MFC_PGM.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-19  |  4.4 KB  |  193 lines

  1. //
  2. //   MFC_PGM is intended as a simple example of the use of
  3. //   the Microsoft Foundation Class (MFC) library with the
  4. //   FTP Client Engine for C/C++ (FCE4C).
  5. //   
  6. //   This example logs onto our FTP site and downloads the
  7. //   file PRODUCTS.TXT.
  8. //
  9. //   Edit PASSWORD with your email address (see line 112 below)
  10. //   before compiling.
  11. //
  12.  
  13. #include "stdafx.h"
  14. #include "resource.h"
  15.  
  16. #include "fce.h"
  17. #include "mfc_pgm.h"
  18.  
  19. static CWnd * MainWndPtr;
  20.  
  21. // CMainWindow: constructor
  22.  
  23. CMainWindow::CMainWindow()
  24. {int Row;
  25.  CString s = "FCE Test";
  26.  LoadAccelTable( "MainAccelTable" );
  27.  Create( NULL, "MFC Example Program",
  28.       WS_OVERLAPPEDWINDOW, rectDefault, NULL, "MainMenu" );
  29.  for(Row=0;Row<NROWS;Row++) Buffer[Row].Empty;
  30.  TheRow = 0;
  31. }
  32.  
  33. // Display character on screen
  34.  
  35. void CMainWindow::DisplayChar(int nChar)
  36. {int Row;
  37.  // process the character
  38.  if(nChar==10) return;
  39.  if(nChar==13)
  40.    {if(TheRow<NROWS-1) TheRow++;
  41.     else
  42.       {// scroll page (TheRow==NROWS-1)
  43.        for(Row=0;Row<=NROWS-2;Row++) Buffer[Row] = Buffer[Row+1];
  44.        Buffer[NROWS-1].Empty();
  45.        TheRow = NROWS-1;
  46.       }
  47.    }
  48.  else
  49.    {// stuff character into display buffer
  50.     Buffer[TheRow] += (char)nChar;
  51.    }
  52. }
  53.  
  54. // Display string on screen
  55.  
  56. void CMainWindow::DisplayString(CString Text)
  57. {
  58.  Buffer[TheRow] += Text;
  59.  Invalidate(TRUE);
  60.  MainWndPtr->UpdateWindow();
  61. }
  62.  
  63. // Display line on screen
  64.  
  65. void CMainWindow::DisplayLine(CString Text)
  66. {
  67.  Buffer[TheRow] += Text;
  68.  DisplayChar(13);
  69.  Invalidate(TRUE);
  70.  MainWndPtr->UpdateWindow();
  71. }
  72.  
  73. // CMainWindow message map
  74.  
  75. BEGIN_MESSAGE_MAP( CMainWindow, CFrameWnd )
  76.    //{{AFX_MSG_MAP( CMainWindow )
  77.    ON_WM_PAINT()
  78.    ON_COMMAND(ID_EXIT,    PgmExit)
  79.    ON_COMMAND(ID_LOGON,   PgmLogon)
  80.    //}}AFX_MSG_MAP
  81. END_MESSAGE_MAP()
  82.  
  83. CTheApp NEAR theApp;
  84.  
  85. // PgmExit: Exits application
  86.  
  87. void CMainWindow::PgmExit(void)
  88. {
  89.  PostQuitMessage(0);
  90. }
  91.  
  92. // display error message 
  93.  
  94. void CMainWindow::ShowError(int Code)
  95. {static char Buffer[65];
  96.  static char Temp[90];
  97.  fceErrorText(0,Code,(LPSTR)Buffer,65);
  98.  wsprintf((LPSTR)Temp,"FCE Error %d: %s\n", Code, (LPSTR)Buffer);
  99.  DisplayLine((LPSTR)Temp);
  100. }
  101.  
  102. // PgmLogon : Log onto FTP site
  103. void CMainWindow::PgmLogon(void)
  104. {int Code;
  105.  ULONG ByteCount;
  106.  char Temp[90];
  107.  CMenu *pMenu;
  108.  
  109. #if 1 
  110. #define SRVR_NAME   "ftp.marshallsoft.com"
  111. #define USER_NAME   "anonymous"
  112. #define PASSWORD    "you@yourisp.com"
  113. #define DIRECTORY   "msc/other"
  114. #define FILE_NAME   "products.txt"
  115. #else
  116. #define SRVR_NAME   "10.0.0.1"
  117. #define USER_NAME   "mike"
  118. #define PASSWORD    "mike"
  119. #define FILE_NAME   "products.txt"
  120. #endif
  121.  
  122.  // disable LogOn menu item
  123.  pMenu = MainWndPtr->GetMenu();
  124.  pMenu->EnableMenuItem(ID_LOGON, MF_BYCOMMAND|MF_GRAYED);
  125.  // attach FCE
  126.  fceAttach(1);
  127.  // define LOG file 
  128.  Code = fceSetString(0,FCE_SET_LOG_FILE,(LPSTR)"mfc_pgm.log");
  129.  // connect to FTP server 
  130.  DisplayString("Connecting to server ");
  131.  DisplayLine(SRVR_NAME); 
  132.  Code = fceConnect(0,(LPSTR)SRVR_NAME,(LPSTR)USER_NAME,(LPSTR)PASSWORD);
  133.  if(Code<0) 
  134.    {ShowError(Code); 
  135.     return;
  136.   }
  137.  DisplayLine("OK");
  138. #ifdef DIRECTORY
  139.  // change to proper directory 
  140.  Code = fceSetServerDir(0, (LPSTR)DIRECTORY);
  141.  if(Code<0) 
  142.    {ShowError(Code); 
  143.     return;
  144.    }
  145. #endif   
  146.  // set to ASCII xfer mode 
  147.  fceSetMode(0,'A');
  148.  // download the file 
  149.  DisplayString("Please wait while file ");
  150.  DisplayString(FILE_NAME);
  151.  DisplayLine(" is being downloaded...");
  152.  Code = fceGetFile(0,(LPSTR)FILE_NAME);           
  153.  if(Code<0) 
  154.    {ShowError(Code); 
  155.     return;
  156.    }
  157.  // display final count 
  158.  ByteCount = fceGetInteger(0, FCE_GET_FILE_BYTES_RCVD);
  159.  wsprintf((LPSTR)Temp,"Download complete. %d bytes received.\n",ByteCount);
  160.  DisplayLine((LPSTR)Temp);
  161.  // QUIT 
  162.  fceClose(0);
  163.  fceRelease();
  164.  Invalidate(TRUE);
  165. }
  166.  
  167. // OnPaint:
  168.  
  169. void CMainWindow::OnPaint()
  170. {int Row;
  171.  CPaintDC dc( this );
  172.  CRect rect;
  173.  GetClientRect( rect );
  174.  dc.SetTextAlign( TA_BASELINE | TA_LEFT );
  175.  dc.SetTextColor( ::GetSysColor( COLOR_WINDOWTEXT ) );
  176.  dc.SetBkMode(TRANSPARENT);
  177.  dc.SelectStockObject(ANSI_FIXED_FONT);
  178.  for(Row=0;Row<=TheRow;Row++)
  179.    {// display the row
  180.     dc.TextOut( 4, 16+(Row<<4), Buffer[Row], Buffer[Row].GetLength() );
  181.    }
  182. }
  183.  
  184. // InitInstance:
  185.  
  186. BOOL CTheApp::InitInstance()
  187. {SetDialogBkColor(); //hook gray dialogs
  188.  m_pMainWnd = new CMainWindow();
  189.  m_pMainWnd->ShowWindow( m_nCmdShow );
  190.  m_pMainWnd->UpdateWindow();
  191.  MainWndPtr = m_pMainWnd;
  192.  return TRUE;
  193. }